<template>
	<view>
		<view v-if="list.length!=0">
			<DiyItem v-if="name=='zujian'" :fenleilist='fenleilist' :list="list" :ismy="true"></DiyItem>
			<MyZhuji v-else-if="name=='zhuji'" @zhujishuaxin='getzhuji()' :items="list"></MyZhuji>
		   <template v-else>
		      <article-item :article="item" v-for="(item,index) in list" :key='index' />
		   </template>
		</view>
		<view v-else>
			<view style="padding: 10px;text-align: center;">
				<image style="margin-top: 30vh;" src="/static/image/meiyou.png"></image>
			</view>
		</view>
	</view>
	
</template>

<script>
	const db=wx.cloud.database()
	const _=db.command
	export default {
		data() {
			return {
				list:[],
				fenleilist:[],
				name:'',
			};
		},
		onLoad(evt) {
			this.name=evt.name
			// this.init()
		},
		onShow() {
				console.log("刷新页面")
				this.init()
		},
		methods:{
			init(){
				if(this.name=='zujian'){
					this.getzujian()
				}else if(this.name=='zhuji'){
					this.getzhuji()
				}else if(this.name=='article'){
					this.getArticle()
					
				}
			},
			getzujian(){
				this.list=[]
				getApp().globalData.userdata.zujian.forEach(item=>{
					db.collection(item.fenlei).where({
						_id:item.id
					}).get().then(res=>{
						this.list.push(res.data[0])
						this.fenleilist.push(item.fenlei)
						console.log(this.list)
					})
				})
			},
			getzhuji(){
				this.list=[]
				db.collection("recommend").where({
					_id:_.in(getApp().globalData.userdata.zhuji)
				}).get().then(res=>{
					this.list=res.data
					console.log(this.list)
				})
			},
		  async getArticle(){
			this.list = [];
			let articleIds = getApp().globalData.userdata.article;
			for(let i=0;i<articleIds.length;i++){
				const res = await db.collection("article").where({
				  _id:articleIds[i]
				}).get()
				// let flag = true;
				// this.list.forEach(item =>{
				// 	if(item._id === res.data[0]._id)
				// 		flag = false;
				// })
				// if(flag)
				this.list.push(res.data[0]);	
			}
		  }
		}
	}
</script>

<style lang="scss">

</style>
